package com.qf.test;

import com.qf.utils.EsClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.ml.EvaluateDataFrameRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;

import java.io.IOException;
import java.text.SimpleDateFormat;


public class CreateIndex {


    public void testCreateIndex() throws IOException {
        RestHighLevelClient client=EsClient.getClient();
        //创建主备份片
        Settings.Builder settings=Settings.builder()
                .put("number_of_shards",5)
                .put("number_of_replices",1);
        //创建索引结构
        XContentBuilder mappings = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("createDate")
                .field("type", "date")
                .endObject()
                .startObject("sendDate")
                .field("type", "date")
                .endObject()
                .startObject("longCode")
                .field("type", "keyword")
                .endObject()
                .startObject("mobile")
                .field("type", "long")
                .endObject()
                .startObject("corpName")
                .field("type", "text")
                .endObject()
                .startObject("smsContent")
                .field("type", "text")
                .endObject()
                .startObject("state")
                .field("type", "boolean")
                .endObject()
                .startObject("operatorId")
                .field("type", "text")
                .endObject()
                .startObject("province")
                .field("type", "text")
                .endObject()
                .startObject("ipAddr")
                .field("type", "ip")
                .endObject()
                .startObject("replyTotal")
                .field("type", "date")
                .endObject()
                .startObject("fee")
                .field("type", "double")
                .endObject()
                .endObject()
                .endObject()
                .endObject();


        //封装成一个索引对象
        CreateIndexRequest request = new CreateIndexRequest("sms-logs-index").settings(settings).mapping(mappings);

        //发送请求并返回响应
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.toString());

    }

}
